package casa.depesos.app.com.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import java.util.Locale;

import casa.depesos.app.com.activity.BankListActivity;
import casa.depesos.app.com.databinding.FirstItemBinding;
import casa.depesos.app.com.databinding.LastItemBinding;
import casa.depesos.app.com.databinding.MidNameItemBinding;
import casa.depesos.app.com.databinding.MidTitleItemBinding;
import casa.depesos.app.com.http.data.Bank;

public class BankListAdapter extends RecyclerView.Adapter {
    List list;
    BankListActivity activity;

    public BankListAdapter(List list, BankListActivity activity) {
        this.list = list;
        this.activity = activity;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            FirstItemBinding binding = FirstItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new FirstHolder(binding);
        } else if (viewType == 1) {
            LastItemBinding binding = LastItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new LastHolder(binding);
        } else if (viewType == 2) {
            MidNameItemBinding binding = MidNameItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new MidNameHolder(binding);
        } else {
            MidTitleItemBinding binding = MidTitleItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new MidTitleHolder(binding);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof FirstHolder) {
            ((FirstHolder) holder).bind(list.get(position).toString());
        } else if (holder instanceof LastHolder) {
            ((LastHolder) holder).bind((Bank) list.get(position));
        } else if (holder instanceof MidNameHolder) {
            ((MidNameHolder) holder).bind((Bank) list.get(position));
        } else if (holder instanceof MidTitleHolder) {
            ((MidTitleHolder) holder).bind(list.get(position).toString());
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        }
        if (position == getItemCount() - 1) {
            return 1;
        }
        Object obj = list.get(position);
        return obj instanceof Bank ? 2 : 3;
    }

    class FirstHolder extends RecyclerView.ViewHolder {
        FirstItemBinding binding;

        public FirstHolder(FirstItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(String s) {
            binding.tv.setText(s);
            binding.tv2.setText(s.toLowerCase(Locale.US));
        }
    }

    class LastHolder extends RecyclerView.ViewHolder {
        LastItemBinding binding;

        public LastHolder(LastItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(Bank item) {
            binding.tv.setText(item.name);
            itemView.setOnClickListener(v -> {
                activity.select(item);
            });
        }
    }

    class MidNameHolder extends RecyclerView.ViewHolder {
        MidNameItemBinding binding;

        public MidNameHolder(MidNameItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(Bank item) {
            binding.tv.setText(item.name);
            itemView.setOnClickListener(v -> {
                activity.select(item);
            });
        }
    }

    class MidTitleHolder extends RecyclerView.ViewHolder {
        MidTitleItemBinding binding;

        public MidTitleHolder(MidTitleItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(String s) {
            binding.tv.setText(s);
            binding.tv2.setText(s.toLowerCase(Locale.US));
        }
    }

}
